package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.content.Intent;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.service.LocationService;
import com.lonwin.inspectionmanagement.app.utils.AppUtil;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.CruiseLocusContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.api.service.BusinessService;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.DelLocusBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HistoryRecordingBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.NeatentLocusBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.OpenLocusBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.PatrolBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.QuestListBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SearLocusBean;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class CruiseLocusPresenter extends BasePresenter<CruiseLocusContract.Model, CruiseLocusContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ConfigBean configBean;
    private  String url="";
    @Inject
    public CruiseLocusPresenter(CruiseLocusContract.Model model, CruiseLocusContract.View rootView,AppManager mAppManager) {
        super(model, rootView);
         configBean= DataHelper.getDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA);
        url=DataUtil.getJNUrl(configBean)+"/hzxh";
    }

    public ConfigBean getConfigBean() {
        return configBean;
    }

    public void setConfigBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    /**
     * 通讯巡查id查询从从定位中心查询轨迹数据所需要的参数
     * @param xcid
     */
    public void getXcpath(String xcid)
    {
        Map<String,String> mpas=new HashMap<>();
        mpas.put("xcid",xcid);
       mModel.getXcpath(DataUtil.getJNUrl(configBean)+"/webmvc/v1/taskhzz/getXcpath",mpas)
               .compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<PatrolBean>(mErrorHandler) {
                   @Override
                   public void onNext(PatrolBean patrolBean) {
                        mRootView.getXcpathSuccess(patrolBean);
                   }
               });
    }
    /**
     * 从定位中心查询轨迹数据
     * @param beginDate
     * @param endDate
     * @param pageNum
     */
    public void  queryHistory(String beginDate, String endDate, int pageNum) {
    Map<String,String> mpas=new HashMap<>();
        mpas.put("1","145523");
        mpas.put("2","hzzzhxxglxt");
        mpas.put("3","2017-09-01 08:00:00");
        mpas.put("4","2018-03-29 23:00:00");

//    mpas.put("1",String.valueOf(configBean.getGpsId()));
//    mpas.put("2",configBean.getProjectId());
//    mpas.put("3",beginDate);
//    mpas.put("4",endDate);
    mpas.put("5",String.valueOf(pageNum));
//    mpas.put("6",String.valueOf(Api.PAGE_SIZE));
        mpas.put("6",String.valueOf(20));

//        DataUtil.getJNUrl(configBean)+
        mModel.queryHistory(DataUtil.getJNUrl(configBean)+"/webmvc/all/StatisticsManage/StatisticsQuerySS/findHistorypathByAdcd",mpas)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<HistoryRecordingBean>(mErrorHandler) {
                    @Override
                    public void onNext(HistoryRecordingBean historyRecordingBean) {
                        mRootView.queryHistorySucess(historyRecordingBean);
                    }
                });
}
//=========================================================================================================================

public void getXcpathTemp(String gjid) {
    Map<String, Object> params=new HashMap<>();
    params.put("gjid",gjid);
    params.put("projectid",configBean.getProjectId());
    mModel.getXcpathTemp(url+"/xcpathTempController/getXcpathTemp",params).compose(RxUtils.applySchedulers(mRootView))
            .subscribe(new Consumer<SearLocusBean>() {
                @Override
                public void accept(SearLocusBean searLocusBean) throws Exception {
                    if (searLocusBean != null && searLocusBean.getCode() == 0) {
                        mRootView.getXcpathTempSuccess(searLocusBean.getData());
                        return;
                    }
                 //   mRootView.getActivity().startService(new Intent(mRootView.getActivity(), LocationService.class));
                    AppUtil.startService(mRootView.getActivity(),new Intent(mRootView.getActivity(), LocationService.class));
                    mRootView.showMessage("查询轨迹失败");
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    //mRootView.getActivity().startService(new Intent(mRootView.getActivity(), LocationService.class));
                    AppUtil.startService(mRootView.getActivity(),new Intent(mRootView.getActivity(), LocationService.class));
                    mRootView.showMessage("查询轨迹失败");
                }
            });
}


    public void neatenXcpathTemp(String gjid,String groupid,String groupnm) {
        Map<String, String> params=new HashMap<>();
        params.put("gjid",gjid);
        params.put("groupid",groupid);
        params.put("groupnm",groupnm);
        mModel.neatenXcpathTemp(url+"/xcpathTempController/neatenXcpathTemp",params).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<NeatentLocusBean>(mErrorHandler) {
                    @Override
                    public void onNext(NeatentLocusBean neatentLocusBean) {
                        if (neatentLocusBean!=null&&neatentLocusBean.getCode()==0)
                        {
                            mRootView.showMessage("编辑成功");
                            mRootView.neatenXcpathTempSuccess(neatentLocusBean.getData());
                            return;
                        }
                        mRootView.showMessage("编辑失败");
                    }
                });
    }


    public void openXcpathTemp(String gjid ) {
        Map<String, Object> params=new HashMap<>();
        params.put("gjid",gjid);
        mModel.openXcpathTemp(url+"/xcpathTempController/openXcpathTemp",params).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<OpenLocusBean>(mErrorHandler) {
                    @Override
                    public void onNext(OpenLocusBean openLocusBean) {
                         if (openLocusBean!=null&&openLocusBean.getCode()==0)
                         {
                             mRootView.showMessage("提交成功");
                             mRootView.openXcpathTempSuccess(openLocusBean.getData());
                             return;
                         }
                         mRootView.showMessage("提交失败");
                    }
                });
    }


    public void deleteXcpathTemp(String gjid ) {
        Map<String, Object> params=new HashMap<>();
        params.put("gjid",gjid);
        mModel.deleteXcpathTemp(url+"/xcpathTempController/deleteXcpathTemp",params).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<DelLocusBean>(mErrorHandler) {
                    @Override
                    public void onNext(DelLocusBean delLocusBean) {
                         if (delLocusBean!=null&&delLocusBean.isData())
                         {
                             mRootView.showMessage("删除成功");
                             mRootView.deleteXcpathTempSuccess(delLocusBean);
                             return;
                         }
                        mRootView.showMessage("删除失败");

                    }
                });
    }

    /**
     *查询这个轨迹的所有点
     * @return
     */
    public void getGcglQuestTemp(String gjid ) {
        Map<String,Object> maps=new HashMap<>();
        maps.put("gjid",gjid );
//        getGcglQuestTemp
         mModel.getGcglQuestTemp(url+"/questTempController/getXcpathlQuestTemp",maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<QuestListBean>(mErrorHandler) {
                    @Override
                    public void onNext(QuestListBean questListBean) {
                      if (questListBean!=null&&questListBean.getData()!=null)
                      {
                          mRootView.getGcglQuestTempSuccess(questListBean.getData());
                          return;
                      }
mRootView.showMessage("没有记录问题");
                    }
                });
    }
        @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
